//User_Login
var User_Login = {
	create: function(){
		var login_form = $("#login-form");
		var login_name = $("#login-name");
		var login_pass = $("#login-pass");
		var login_alert = $("#login-alert");
		var login_submit = $("#login-submit");

		var user_login = {};
		
		user_login.submitUrl="";
		user_login.successUrl="";
		
		user_login.onSubmit = function(){
			if(login_name.val() == ""){
				showAlert("请输入用户名");
				return false;
			}
			if(login_pass.val() == ""){
				showAlert("请输入密码");
				return false;
			}
			
			login_submit.bs_button('loading');
			

			login_form.ajaxSubmit({
				type:"post",
				url:user_login.submitUrl,
				success:function(data){
					try{
						if(typeof(data.data) != "string"){
							showAlert("返回数据错误");
							login_submit.bs_button('reset');
						}else{
							if(data.success == 1){
								document.location.href = user_login.successUrl;
							}else{
								showAlert(data.data); 
								login_submit.bs_button('reset');          
							}
						}
					}catch(err){
						showAlert("返回数据错误");
						login_submit.bs_button('reset');
				  };
				},
				error:function(){
					showAlert("服务器或网络错误");
					login_submit.bs_button('reset');
				}
			});

			
			
			return false;
		};
		
		function showAlert(text){
			login_alert.fadeIn("fast");
			login_alert.html(text);
		}
		return user_login;
	}
};



function modal_submit(submitUrl,form,alert,button,initial,complete){
	button = $(button);
	initial = $(initial);
	complete = $(complete);
	button.button('loading');
	$(form).ajaxSubmit({
		type:'post',
		url:submitUrl,
		success:function(data){
			try{
				if(typeof(data.data) != "string"){
					showModalResult(alert,false,"返回数据错误");
					button.button('reset');
				}else{
					if(data.success == 1){
						showModalResult(alert,true,data.data);
						button.button('reset');
						complete.show();
						initial.hide();
						onRefrese();
					}else{
						showModalResult(alert,false,data.data); 
						button.button('reset');          
					}
				}
			}catch(err){
		        //console.log(err);
		        showModalResult(alert,false,"返回数据错误");
		        button.button('reset');
		    }
		    button.attr('disabled','disabled');
		},
		error:function(){
			showModalResult(alert,false,"服务器错误");
			button.button('reset');
		}
	});
}

function showModalResult(alert,success,text){
	alert = $(alert);
	alert.html(text);
	if(success)
		alert.removeClass("alert-danger").addClass("alert-success");
	else
		alert.removeClass("alert-success").addClass("alert-danger");

	alert.fadeIn();     
}


function showAlert(success,title,text){
	var alert_div = $("#alert-modal .alert");
	alert_div.html(text);
	if(success)
		alert_div.removeClass("alert-danger").addClass("alert-success");
	else
		alert_div.removeClass("alert-success").addClass("alert-danger");

	$("#alert-modal .modal-title").text(title);
	$("#alert-modal").modal('show');
}

function creatPage(count,perpage,page,pageFunction){
	if(isNaN(perpage) || isNaN(page)){
		return;
	}
	page = parseInt(page); 
	var page_ul = $("<ul class=\"pagination\"></ul>");

	var pageNum = Math.ceil(count/perpage);
	if(page>1)
		page_ul.append("<li><a page=\""+(page-1)+"\">&laquo;</a></li>");
	else
		page_ul.append("<li class=\"disabled\"><a href=\"#\">&laquo;</a></li>");

	if(pageNum > 1){
		var start = 0;
		var end = pageNum;
		if(page<3){
			start = 1;
			end = 5;
		}else if(page > pageNum-2){
			start = pageNum-4;
			end = pageNum;
		}else{
			start = page-2;
			end = page+2;
		}
		for (var i = start; i <= end; i++){
			if(i>0 && i<=pageNum){
				if(i == page)
					page_ul.append("<li class=\"active\"><span class=\"current\">"+i+"</span></li>");
				else
					page_ul.append("<li><a page=\""+i+"\">"+i+"</a></li>");
			}
		}
	}

	if(page<pageNum)
		page_ul.append("<li><a page=\""+(page*1+1)+"\">&raquo;</a></li>");			
	else                                                                                                               
		page_ul.append("<li class=\"disabled\"><a href=\"#\">&raquo;</a></li>");

	page_ul.find("a[page]").click(function(e){
		pageFunction($(e.target).attr("page"));
	});
	return page_ul;
}